const Mock = require('better-mock/dist/mock.mp')
const {
    param2Obj
} = require('./utils')
const userMock = require('./user')
const appMock = require('./app')

const mocks = [...appMock, ...userMock]

function mockXHR() {
    function XHR2ExpressReqWrap(respond) {
        return function(options) {
            let result = null
            if (respond instanceof Function) {
                const {
                    body,
                    type,
                    url
                } = options
                result = respond({
                    method: type,
                    // body: JSON.parse(body),
                    body: body,
                    query: param2Obj(url),
                })
            } else {
                result = respond
            }
            return Mock.mock(result)
        }
    }

    for (const i of mocks) {
        Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
    }
}


module.exports = {
    mocks,
    mockXHR
}